//
//  TabViewController.m
//  SP2P
//
//  Created by EIMS-IOS on 15/12/17.
//  Copyright © 2015年 EIMS. All rights reserved.
//

#import "TabViewController.h"
#import "HomeViewController.h"
#import "InvestmentViewController.h"
#import "AccountCenterViewController.h"
#import "SGLoginViewController.h"
#import "BarButtonItem.h"
#import "InviteFriendsViewController.h"

@interface TabViewController ()<SGBackDelegate>

@property (nonatomic,strong)UIImageView *tabBarView;
@end

@implementation TabViewController

+(TabViewController*)shareTableView
{
    static TabViewController *_shareTableView = nil;
    
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        
        _shareTableView = [[TabViewController alloc] init];
        
    });
    
    return _shareTableView;
    
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.tabBar.hidden = NO;
    self.delegate = self;
    
    
    HomeViewController *homeVC = [[HomeViewController alloc] init];
    homeVC.view.backgroundColor = KbackgroundColor;
    InvestmentViewController *investmentVC = [[InvestmentViewController alloc] init];
    investmentVC.view.backgroundColor = KbackgroundColor;
    
    AccountCenterViewController *accountCenterVC = [[AccountCenterViewController alloc] initWithNibName:@"AccountCenterViewController" bundle:nil];
    accountCenterVC.view.backgroundColor = KbackgroundColor;
    
    InviteFriendsViewController *inviteVC = [[InviteFriendsViewController alloc] init];
    inviteVC.view.backgroundColor = KbackgroundColor;
    
    
    UINavigationController *homeNV= [[UINavigationController alloc] initWithRootViewController:homeVC];
    UINavigationController *investmentNV = [[UINavigationController alloc] initWithRootViewController:investmentVC];
    UINavigationController *myAccountVN = [[UINavigationController alloc] initWithRootViewController:accountCenterVC];
    UINavigationController *inviteVN= [[UINavigationController alloc] initWithRootViewController:inviteVC];
    
    
    [self initNavigationBar:homeNV];
    [self initNavigationBar:investmentNV];
    [self initNavigationBar:inviteVN];
    [self initNavigationBar:myAccountVN];

    
    NSMutableArray *controllers = [[NSMutableArray alloc]init];
    
    [controllers addObject:homeNV];
    [controllers addObject:investmentNV];
    [controllers addObject:inviteVN];
    [controllers addObject:myAccountVN];
    
    
    self.tabBar.tintColor = KColor;
    self.viewControllers = controllers;
    self.selectedIndex = 0;
    
    
    UITabBarItem *tabBarItem0 = [self.tabBar.items objectAtIndex:0];
    UITabBarItem *tabBarItem1 = [self.tabBar.items objectAtIndex:1];
    UITabBarItem *tabBarItem2 = [self.tabBar.items objectAtIndex:2];
    UITabBarItem *tabBarItem3 = [self.tabBar.items objectAtIndex:3];
    

    tabBarItem0.title = NSLocalizedString(@"Tab_Home", nil);
    tabBarItem1.title = NSLocalizedString(@"Tab_Investment", nil);
    tabBarItem2.title = NSLocalizedString(@"Tab_Invite", nil);
    tabBarItem3.title = NSLocalizedString(@"Tab_Account", nil);
    
  
    tabBarItem0.image = [[UIImage imageNamed:@"tab_home"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    tabBarItem1.image = [[UIImage imageNamed:@"tab_investment"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    tabBarItem2.image = [[UIImage imageNamed:@"tab_invite"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    tabBarItem3.image = [[UIImage imageNamed:@"tab_account"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    

    tabBarItem0.selectedImage = [[UIImage imageNamed:@"tab_home_selected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    tabBarItem1.selectedImage = [[UIImage imageNamed:@"tab_invest_selected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    tabBarItem2.selectedImage = [[UIImage imageNamed:@"tab_invite_selected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    tabBarItem3.selectedImage = [[UIImage imageNamed:@"tab_account_selected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
}

- (void)initNavigationBar:(UINavigationController *)navigationController
{
    [UIApplication sharedApplication].statusBarHidden = NO;
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    [navigationController.navigationBar setBarTintColor:KColor];
}

- (void)leftClick:(id)sender{
    
}

-(void)setSelectedIndex:(NSUInteger)selectedIndex
{
    //判断是否相等,不同才设置
    if (self.selectedIndex != selectedIndex) {
        //设置最近一次
        _lastSelectedIndex = self.selectedIndex;
    }
    //调用父类的setSelectedIndex
    [super setSelectedIndex:selectedIndex];
}

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    //获得选中的item
    NSUInteger tabIndex = [tabBar.items indexOfObject:item];
    if (tabIndex != self.selectedIndex) {
    //设置最近一次变更
        _lastSelectedIndex = self.selectedIndex;
    }
}

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    //判断的是当前点击的tabBarItem的标题
    if ([viewController.tabBarItem.title isEqualToString:NSLocalizedString(@"Tab_Account", nil)] || [viewController.tabBarItem.title isEqualToString:NSLocalizedString(@"Tab_Invite", nil)]) {
        if(AppDelegateInstance.userInfo == nil) {
            //跳到登录页面
            SGLoginViewController *login = [[SGLoginViewController alloc] init];
            UINavigationController *loginNav = [[UINavigationController alloc]initWithRootViewController:login];
            login.delegate = self;
            //login.backType = AccountCenter;
            [((UINavigationController *)tabBarController.selectedViewController) presentViewController:loginNav animated:YES completion:nil];
            return NO;
            
        }
    }
    
    return YES;
}

#pragma mark - 返回上一个tabbar
- (void)backAction {
    [self setSelectedIndex:_lastSelectedIndex];
}
@end
